package com.sky.JWT;

import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTCreator;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.interfaces.DecodedJWT;
import com.sky.entity.Employee;

import java.util.Date;

public class JWTUtils {

    private static final long EXPIRE = 30 * 60 * 1000;
    private static final String SECRET = "!ad#12~";

    public static String getToken(Employee employee) {
        JWTCreator.Builder builder = JWT.create();
        return builder.withClaim("username", employee.getUsername())
                .withClaim("role", employee.getRole())
                .withClaim("id",employee.getId())
                .withExpiresAt(new Date(System.currentTimeMillis() + EXPIRE))
                .sign(Algorithm.HMAC256(SECRET));
    }

    public static DecodedJWT verify(String token) {
        return JWT.require(Algorithm.HMAC256(SECRET)).build().verify(token);
    }

}

